﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Main_Reports_ServiceInvoice_InvoiceViews_Outstandings : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    AccountSettings acntstng = new AccountSettings();
    DataTable dtMain = new DataTable();
    DataTable dt = new DataTable();

    Double totalAmount = 0;
    Double totalRefAmount = 0;
  
    string CrDrOriginalN = string.Empty;
    string CrDrPendingN = string.Empty;

    string finyear = "";
    int count = 0;
    //string str = string.Empty;
    string CrDrOriginal = string.Empty;
    string CrDrPending = string.Empty;
    int chek = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {
            finyear = Session["finyear"].ToString();
            hidfinyear.Value = Session["finyear"].ToString();
            //txtFromDate.Text = Convert.ToString("01/04/" + finyear);
            string strToDate = "31/03/" + (Convert.ToInt32(finyear) + 1).ToString();
            txtToDate.Text = strToDate;
            hidToDate.Value = strToDate;

            DataTable DTBind = _GetOutstandingData(strToDate);

            lblMsg.Text = "Outstanding Register : Till " + string.Format("{0:dd-MMM-yyyy}",Convert.ToDateTime(strToDate)) + ".";
            lblMsg2.Text = "Outstanding Register : Till " + string.Format("{0:dd-MMM-yyyy}",Convert.ToDateTime(strToDate)) + ".";
            DataView dv = DTBind.DefaultView;

            string[] strColumns = { "AgeRange" };
            DataTable dt = dv.ToTable(true, strColumns);

            DataList1.DataSource = dt; // dtMain;
            DataList1.DataBind();
            DataList2.DataSource = dt; // dtMain;
            DataList2.DataBind();
            lblCompName.Text = Session["compname"].ToString();
            lblCompName2.Text = Session["compname"].ToString();
        }
    }
    private DataTable _GetOutstandingData(string strTillDate)
    {
        DataTable DTReturn = new DataTable();
        if (strTillDate != string.Empty)
        {
            SqlParameter[] sqlparam = {
                                      new SqlParameter("@TODATE",string.Format("{0:MM/dd/yyyy}",Convert.ToDateTime(strTillDate))),
                                      };

            DTReturn = DU.getdatausingprocedure("USP_IVC_GetBillwiseOutstanding", sqlparam);
        }
      
        return DTReturn;
    }

    protected void grdChild_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridView grdChild = (GridView)sender;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Double amnt = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "Amount"));
            Double Refamnt = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "RefAmt"));
            //totalAmount = totalAmount + amnt;
            //totalRefAmount = totalRefAmount + Refamnt;

            if (amnt > 0)
            {
               CrDrOriginal= " Cr";
            }
            else if (amnt < 0)
            {
                CrDrOriginal = " Dr";
            }
            else
            {
                CrDrOriginal = string.Empty;
            }

            if (Refamnt > 0)
            {
               CrDrPending = " Cr";
            }
            else if (amnt < 0)
            {
                CrDrPending = " Dr";
            }
            else
            {
                CrDrPending = string.Empty;
            }
            if (Math.Abs(amnt) > 0)
            {
                e.Row.Cells[4].Text = acntstng.GetCurrency(Math.Abs(amnt)).ToString() + CrDrOriginal ;
            }
            else
            {
                e.Row.Cells[4].Text = string.Empty;
            }
            if (Math.Abs(Refamnt) > 0)
            {
                e.Row.Cells[5].Text = acntstng.GetCurrency(Math.Abs(Refamnt)).ToString()+CrDrPending;
            }
            else
            {
                e.Row.Cells[5].Text = string.Empty;
            }
            e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[5].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[6].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[7].HorizontalAlign = HorizontalAlign.Right;
        }
    }

    protected void btnCangeDate_Click(object sender, EventArgs e)
    {
        hidToDate.Value = txtToDate.Text;
        DataTable DTBind = _GetOutstandingData(txtToDate.Text);

        DataView dv = DTBind.DefaultView;

        string[] strColumns = { "AgeRange" };
        DataTable dt = dv.ToTable(true, strColumns);
      
        DataList1.DataSource = dt; // dtMain;
        DataList1.DataBind();
        DataList2.DataSource = dt; // dtMain;
        DataList2.DataBind();
    }

    protected void DataList1_ItemBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            ImageButton imgShow = (ImageButton)e.Item.FindControl("imgShow");
            GridView grd = (GridView)e.Item.FindControl("grdDetails");
            Label lblAgeRange = (Label)e.Item.FindControl("lblAgeRange");
            imgShow.Attributes.Add("onclick", "return expandDetailsInGrid('" + imgShow.ClientID + "','" + grd.ClientID + "');");

            DataTable DTTempData = _GetOutstandingData(hidToDate.Value);
            DataView dv = DTTempData.DefaultView;
            string strr = "AgeRange ='" + lblAgeRange.Text + "'";
            dv.RowFilter = strr;
            DataTable des = dv.ToTable();
            grd.DataSource = dv.ToTable();
            grd.DataBind();

            grd.RowDataBound += new GridViewRowEventHandler(grdChild_RowDataBound);
          
        }
    }
       
    protected void DataList2_ItemBound(object sender, DataListItemEventArgs e)
    {
        GridView grd = (GridView)e.Item.FindControl("grdPrint");
        Label lblPrintAgeRange = (Label)e.Item.FindControl("lblPrintAgeRange");
        totalAmount = 0;
        totalRefAmount = 0;
        DataTable DTTempData = _GetOutstandingData(hidToDate.Value);
        DataView dv = DTTempData.DefaultView;
        string str = "AgeRange ='" + lblPrintAgeRange.Text + "'";       
        dv.RowFilter = str;
        DataTable des = dv.ToTable();
        grd.DataSource = dv.ToTable();
        grd.DataBind();
        
        grd.RowDataBound += new GridViewRowEventHandler(grdPrint_RowDataBound);
       //imgShow.Attributes.Add("onclick", "return expandDetailsInGrid('" + imgShow.ClientID + "','" + grd.ClientID + "');");
       
    }
    protected void grdPrint_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridView grdChild = (GridView)sender;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Double amnt = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "Amount"));
            Double Refamnt = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "RefAmt"));
            totalAmount = totalAmount + amnt;
            totalRefAmount = totalRefAmount + Refamnt;

            if (amnt > 0)
            {
                CrDrOriginalN = " Cr";
            }
            else if (amnt < 0)
            {
                CrDrOriginalN = " Dr";
            }
            else
            {
                CrDrOriginalN = string.Empty;
            }

            if (Refamnt > 0)
            {
                CrDrPendingN = " Cr";
            }
            else if (amnt < 0)
            {
                CrDrPendingN = " Dr";
            }
            else
            {
                CrDrPendingN = string.Empty;
            }
            if (Math.Abs(amnt) > 0)
            {
                e.Row.Cells[3].Text = acntstng.GetCurrency(Math.Abs(amnt)).ToString() + CrDrOriginal;
            }
            else
            {
                e.Row.Cells[3].Text = string.Empty;
            }
            if (Math.Abs(Refamnt) > 0)
            {
                e.Row.Cells[4].Text = acntstng.GetCurrency(Math.Abs(Refamnt)).ToString() + CrDrPending;
            }
            else
            {
                e.Row.Cells[4].Text = string.Empty;
            }
            e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Right;
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[2].Text = "Total";
            e.Row.Cells[3].Text = acntstng.GetCurrency(Math.Abs(totalAmount));
            e.Row.Cells[4].Text = acntstng.GetCurrency(Math.Abs(totalRefAmount));
            e.Row.Font.Bold = true;
            e.Row.HorizontalAlign = HorizontalAlign.Right;
            e.Row.VerticalAlign = VerticalAlign.Bottom;
        }
    }
}
